package com.ruoyi.puyu.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.puyu.domain.PuyuMemberIntegral;
import com.ruoyi.puyu.service.IPuyuMemberIntegralService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 队员积分Controller
 * 
 * @author wusongwen
 * @date 2024-12-18
 */
@RestController
@RequestMapping("/puyu/memberintegral")
public class PuyuMemberIntegralController extends BaseController
{
    @Autowired
    private IPuyuMemberIntegralService puyuMemberIntegralService;

    /**
     * 查询队员积分列表
     */
    @PreAuthorize("@ss.hasAnyPermi('puyu:memberintegral:list,puyu:integral:list')")
    @GetMapping("/list")
    public TableDataInfo list(PuyuMemberIntegral puyuMemberIntegral)
    {
        if (SecurityUtils.hasRole(UserConstants.ROLE_MANAGER)
                || SecurityUtils.hasRole(UserConstants.ROLE_ADMIN)) {
            puyuMemberIntegral.setUserId(null);
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_PARENT)) {
            puyuMemberIntegral.getParams().put("parentUserId", SecurityUtils.getUserId());
            puyuMemberIntegral.setUserId(null);
            puyuMemberIntegral.setDelFlag("0");
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_STUDENT)) {
            puyuMemberIntegral.setUserId(SecurityUtils.getUserId());
            puyuMemberIntegral.setDelFlag("0");
        } else if (SecurityUtils.hasRole(UserConstants.ROLE_OUTSIDE)) {
            puyuMemberIntegral.setUserId(SecurityUtils.getUserId());
            puyuMemberIntegral.setDelFlag("0");
        }
        startPage();
        List<PuyuMemberIntegral> list = puyuMemberIntegralService.selectPuyuMemberIntegralList(puyuMemberIntegral);
        return getDataTable(list);
    }

    /**
     * 积分排名，无权限控制
     */
    @GetMapping("/sort")
    public TableDataInfo sort(PuyuMemberIntegral puyuMemberIntegral)
    {
        startPage();
        List<PuyuMemberIntegral> list = puyuMemberIntegralService.selectPuyuMemberIntegralList(puyuMemberIntegral);
        return getDataTable(list);
    }

    /**
     * 积分排名，无权限控制
     */
    @GetMapping("/all")
    public TableDataInfo all(PuyuMemberIntegral puyuMemberIntegral)
    {
        List<PuyuMemberIntegral> list = puyuMemberIntegralService.selectPuyuMemberIntegralSort(puyuMemberIntegral);
        return getDataTable(list);
    }

    /**
     * 导出队员积分列表
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:export')")
    @Log(title = "队员积分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PuyuMemberIntegral puyuMemberIntegral)
    {
        List<PuyuMemberIntegral> list = puyuMemberIntegralService.selectPuyuMemberIntegralList(puyuMemberIntegral);
        ExcelUtil<PuyuMemberIntegral> util = new ExcelUtil<PuyuMemberIntegral>(PuyuMemberIntegral.class);
        util.exportExcel(response, list, "队员积分数据");
    }

    /**
     * 获取队员积分详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:query')")
    @GetMapping(value = "/{memberId}")
    public AjaxResult getInfo(@PathVariable("memberId") Long memberId)
    {
        return success(puyuMemberIntegralService.selectPuyuMemberIntegralByMemberId(memberId));
    }

    /**
     * 获取队员积分详细信息
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:query')")
    @PostMapping(value = "/getMemberByPhoneAndName")
    public AjaxResult getMemberByPhoneAndName(@RequestBody PuyuMemberIntegral puyuMemberIntegral)
    {
        return success(puyuMemberIntegralService.selectPuyuMemberIntegralByPhoneAndName(puyuMemberIntegral));
    }

    /**
     * 新增队员积分
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:add')")
    @Log(title = "队员积分", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PuyuMemberIntegral puyuMemberIntegral)
    {
        return toAjax(puyuMemberIntegralService.insertPuyuMemberIntegral(puyuMemberIntegral));
    }

    /**
     * 新增队员积分
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:add')")
    @Log(title = "队员积分", businessType = BusinessType.INSERT)
    @PostMapping("/save")
    public AjaxResult save(@RequestBody PuyuMemberIntegral puyuMemberIntegral)
    {
        return toAjax(puyuMemberIntegralService.savePuyuMemberIntegral(puyuMemberIntegral));
    }

    /**
     * 修改队员积分
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:edit')")
    @Log(title = "队员积分", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PuyuMemberIntegral puyuMemberIntegral)
    {
        return toAjax(puyuMemberIntegralService.updatePuyuMemberIntegral(puyuMemberIntegral));
    }

    /**
     * 删除队员积分
     */
    @PreAuthorize("@ss.hasPermi('puyu:memberintegral:remove')")
    @Log(title = "队员积分", businessType = BusinessType.DELETE)
	@DeleteMapping("/{memberIds}")
    public AjaxResult remove(@PathVariable Long[] memberIds)
    {
        return toAjax(puyuMemberIntegralService.deletePuyuMemberIntegralByMemberIds(memberIds));
    }
}
